package com.bootdo.system.controller;

import cn.hutool.core.date.DateUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.CheckLogDO;
import com.bootdo.system.service.CheckLogService;
import com.bootdo.system.vo.CheckLogVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 安全检查记录表
 *
 * @author huizhang
 * @email
 * @date 2021-01-25 16:09:17
 */

@Controller
@RequestMapping("/system/checkLog")
public class CheckLogController {
    @Autowired
    private CheckLogService checkLogService;

    @GetMapping()
    @RequiresPermissions("system:checkLog:checkLog")
    String CheckLog() {
        return "system/checkLog/checkLog";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:checkLog:checkLog")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<CheckLogVO> checkLogList = checkLogService.list(query);
        int total = checkLogService.count(query);
        PageUtils pageUtils = new PageUtils(checkLogList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("system:checkLog:add")
    String add() {
        return "system/checkLog/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("system:checkLog:edit")
    String edit(@PathVariable("id") String id, Model model) {
        CheckLogVO checkLog = checkLogService.get(id);
        checkLog.setCheckTimeStr(DateUtil.format(checkLog.getCheckTime(),"yyyy-MM-dd HH:mm:ss"));
        checkLog.setCreateTimeStr(DateUtil.format(checkLog.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
        model.addAttribute("checkLog", checkLog);
		model.addAttribute("attachId", UUID.randomUUID());
        return "system/checkLog/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:checkLog:add")
    public R save(CheckLogDO checkLog) {
        if (checkLogService.save(checkLog) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("system:checkLog:edit")
    public R update(CheckLogDO checkLog) {
        checkLogService.update(checkLog);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:checkLog:remove")
    public R remove(String id) {
        if (checkLogService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:checkLog:batchRemove")
    public R remove(@RequestParam("ids[]") String[] ids) {
        checkLogService.batchRemove(ids);
        return R.ok();
    }

}
